<template>
  <div class="flex flex-col justify-center items-center">
    <div>
      <vue-okr-tree
        node-key="id"
        :default-expanded-keys="defaultExpandKes"
        :data="testData"
        show-collapsable
        animate
        @node-click="handleNodeClick"
        @node-expand="onNodeExpand"
        :node-btn-content="renderContent"
      ></vue-okr-tree>
    </div>
    <div>
      <vue-okr-tree
        direction="horizontal"
        node-key="id"
        :data="subTree"
        show-collapsable
        animate
        @node-expand="onNodeExpand"
        :node-btn-content="renderContent"
      ></vue-okr-tree>
    </div>
  </div>
</template>

<script>
import { VueOkrTree } from "vue-okr-tree";
import { nextTick } from "vue";
import "vue-okr-tree/dist/vue-okr-tree.css";
import { buildTreeNodeMap } from "./util.js";
let lastCurNode = null;

function buildBtnId(dataId) {
  return `custom-okr-btn-${dataId}`;
}
const tree = [
  {
    id: 1,
    label: "xxx科技有有限公司",
    children: [
      {
        id: 2,
        label: "产品研发部",
        children: [
          {
            id: 3,
            label: "研发-前端",
          },
          {
            id: 4,
            label: "研发-后端",
          },
          {
            id: 5,
            label: "UI 设计",
          },
        ],
      },
      {
        id: 6,
        label: "销售部",
        children: [
          {
            id: 7,
            label: "销售一部",
          },
          {
            id: 8,
            label: "销售二部",
            children: [
              {
                id: 81,
                label: "销售二部--第一组",
              },
            ],
          },
        ],
      },
      {
        id: 9,
        label: "财务部",
      },
    ],
  },
];
const treeNodeMap = buildTreeNodeMap(tree);
export default {
  components: { VueOkrTree },

  data() {
    return {
      defaultExpandKes: [4],
      testData: tree,
      subTree: [],
      treeNodeMap,
    };
  },
  methods: {
    buildTreeNodeMap(treeNode) {
      const map = new Map();
      this.doBuildTreeNodeMap(treeNode, map);
      return map;
    },
    doBuildTreeNodeMap(treeNodeArr, map, parentNode) {
      for (let treeNode of treeNodeArr) {
        map.set(treeNode.id, treeNode);
        if (!treeNode.ancestorIds) {
          treeNode.ancestorIds = [];
        }
        if (parentNode) {
          treeNode.ancestorIds = [...parentNode.ancestorIds, parentNode.id];
        }
        if (treeNode.children && treeNode.children.length > 0) {
          this.doBuildTreeNodeMap(treeNode.children, map, treeNode);
        }
      }
    },
    renderContent(h, node) {
      const id = buildBtnId(node.data.id);
      return (
        <div class="org-chart-node-btn-text" id={id}>
          {node.childNodes.length || 0}
        </div>
      );
    },
    handleNodeClick(data) {
      console.log("data", data);
      this.subTree = data.children ? data.children : [];
    },
    onNodeExpand(nodeData, node) {
      // 获取所有节点id集合(无论原本id是什么类型，这里都会是字符串类型)
      const allIds = Object.keys(node.store.nodesMap);
      // 除了当前节点以及当前节点的先辈节点，不要收缩外，其他节点全部收缩
      const notCloseNodeIds = [...nodeData.ancestorIds, nodeData.id];
      for (let id of allIds) {
        if (notCloseNodeIds.indexOf(parseInt(id)) < 0) {
          // 非当前节点的先辈节点
          node.store.nodesMap[id].expanded = false;
        }
      }
    },
  },
};
</script>
<style>
/**按钮文 */
.org-chart-node-btn-text {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  border-radius: 50%;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909090;
  z-index: 2;
}
</style>
